package com.ktjy.web.dao.impl;

import com.ktjy.web.dao.BaseDao;
import com.ktjy.web.dao.OrderListDao;
import com.ktjy.web.entity.OrderList;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class OrderListDaoImpl extends BaseDao implements OrderListDao {
    @Override
    public List<OrderList> selectAll() {
        List<OrderList> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement patsm = null;
        ResultSet rs = null;
        try{
            //1.创建数据库连接
            conn = getConnection();
            //2.编写sql语句
            String sql = "select t.id as tid,t.order_sn as torder,adds.user_name as userName,adds.tel_number as number,t.order_state as state,adds.fullLocation as adds,t.order_price as order_price,t.actual_price as actual_price,t.create_time as create_time,t.update_time as update_time from t_order as t left join t_wx_user as wx on t.user_id = wx.id  left join t_address as adds on t.address_id = adds.id  where t.enabled=0;";

            //3.创建执行对象
            patsm = conn.prepareStatement(sql);
            //4.执行
            rs = patsm.executeQuery();
            //5.处理执行结果
            while (rs.next()){
                OrderList tOrder = new OrderList();
                tOrder.setId(rs.getInt("tid"));
                tOrder.setOrder_sn(rs.getString("torder"));
                tOrder.setUserName(rs.getString("userName"));
                tOrder.setNumber(rs.getString("number"));
                tOrder.setOrder_state(rs.getInt("state"));
                tOrder.setFullLocation(rs.getString("adds"));
                tOrder.setOrder_price(rs.getDouble("order_price"));
                tOrder.setActual_price(rs.getDouble("actual_price"));
                tOrder.setCreate_time(rs.getString("create_time"));
                tOrder.setUpdate_time(rs.getString("update_time"));
                list.add(tOrder);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            //6.释放资源
            closeAll(rs,patsm,conn);
        }
        return list;
    }

    @Override
    public List<OrderList> selectById(Integer orde_id) {
        List<OrderList> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement patsm = null;
        ResultSet rs = null;
        try{
            //1.创建数据库连接
            conn = getConnection();
            //2.编写sql语句
            String sql = "select t1.goods_id as goods_id, goods_name,goods_img,goods_desc,goods_price,attrsText,quantity from t_order_detail as t1 left join t_order as t2 on t1.order_id=t2.id left join t_sku_info as sku on t1.sku_id = sku.id left join t_goods_info as good on t1.goods_id = good.id where t2.enabled = 0 and t1.order_id=?";

            //3.创建执行对象
            patsm = conn.prepareStatement(sql);
            patsm.setInt(1,orde_id);
            //4.执行
            rs = patsm.executeQuery();
            //5.处理执行结果
            while (rs.next()){
                OrderList tOrder = new OrderList();
                tOrder.setGoods_id(rs.getInt("goods_id"));
                tOrder.setGoods_name(rs.getString("goods_name"));
                tOrder.setGoods_img(rs.getString("goods_img"));
                tOrder.setGoods_desc(rs.getString("goods_desc"));
                tOrder.setGoods_price(rs.getDouble("goods_price"));
                tOrder.setAttrsText(rs.getString("attrsText"));
                tOrder.setQuantity(rs.getInt("quantity"));
                list.add(tOrder);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            //6.释放资源
            closeAll(rs,patsm,conn);
        }
        return list;
    }
}
